﻿using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;
using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZhwangTool;
using Forms = System.Windows.Forms;

namespace ZhwangTool_Gao
{
    /// <summary>
    /// CalUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class CalUserControl : UserControl
    {
        public CalUserControl()
        {
            InitializeComponent();

            //LoadChartPlotter();
        }
        private ObservableDataSource<Point> currentDataFrame = new ObservableDataSource<Point>();

        //private void LoadChartPlotter()
        //{
        //    LineGraph graphAcc_x = new LineGraph();

        //    //注册绘图图线，配置粗细颜色以及显示名称
        //    graphAcc_x = plotter.AddLineGraph(currentDataFrame, Colors.Red, 1, "AD_data");


        //    plotter.FitToView();

        //    //ShowLine();
        //}



        private void Button1_Click(object sender, RoutedEventArgs e)
        {
  
            
        }


        List<DmxShuJu> dmxs = new List<DmxShuJu>();

        string dpfFileName = string.Empty;


        Road road=new Road();




        /// <summary>
        /// 从dpf文件导入竖曲线数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, RoutedEventArgs e)
        {

            #region 获取DPF文件名

            var OFD = new Forms.OpenFileDialog();

            OFD.Filter = "DicadPro 数据文件(*.dpf)|*.dpf|All Files (*.*)|*.*";

            OFD.FilterIndex = 0;

            OFD.ShowDialog();

            if (File.Exists(OFD.FileName) == false)
            {
                return;
            }
            else
            {
                textBox1.Text = OFD.FileName;

                dpfFileName = OFD.FileName;

            }
            #endregion

            listBox1.Items.Clear();

            currentDataFrame.Collection.Clear();



            DGVA.ItemsSource = null;
            DGVB.ItemsSource = null;
            DGVC.ItemsSource = null;

            if (File.Exists(dpfFileName) == false)
            {
                return;
            }

            road = new Road(dpfFileName);


            //断链相关处理
            if (road.HasDl == true)
            {
                DGVC.Visibility = Visibility.Visible;

                DGVC.ItemsSource = road.Dl;

                listBox1.IsEnabled = true;

                List<LuDuan> lds = road.Ld;

                foreach (var item in road.Ld)
                {
                    listBox1.Items.Add(item.ToString());

                    //comboBox16.Items.Add(item.ToString());

                }
                listBox1.SelectedIndex = 0;

                DGVC.Visibility = Visibility.Visible;

                textDL.Visibility = Visibility.Visible;

                listBox1.Visibility = Visibility.Visible;

                DGVA.SetValue(Grid.RowSpanProperty, 1);

                DGVB.SetValue(Grid.RowSpanProperty, 1);

            }
            else
            {
                DGVC.Visibility = Visibility.Collapsed;

                textDL.Visibility = Visibility.Collapsed;

                listBox1.Visibility = Visibility.Collapsed;

                DGVA.SetValue(Grid.RowSpanProperty, 4);

                DGVB.SetValue(Grid.RowSpanProperty, 4);

                listBox1.Items.Clear();

                listBox1.Items.Add("桩号是唯一的");

                listBox1.SelectedIndex = 0;

            }

            if (road.Bps?.Count > 0)
            {
                DGVA.ItemsSource = road.Bps;

                Textbox3.Text = road.MinZdmZh.ToString();
            }

            if (road.Dmx?.Count > 0)
            {
                DGVB.ItemsSource = road.Dmx;
            }

            Button22_Click(sender, e);

            //TextBox1  平均计算高的起点桩号

            TextBox40.Text = road.Bps[0].ZhuangHao.ToString();

            //TextBox14  平均计算高的终点桩号

            TextBox41.Text = road.Bps[road.Bps.Count - 1].ZhuangHao.ToString();

            //陡缓查询的起终点范围

            textBox8.Text = road.Bps[0].ZhuangHao.ToString();

            textBox9.Text = road.Bps[road.Bps.Count - 1].ZhuangHao.ToString();

            //处理绘图部分
            
            DrawCurve();

        }

        internal void DrawCurve()
        {
            Point point = new Point(0, 0);

            List<DataPoint> pts = new List<DataPoint>();

            for (int i = 0; i < road.RealBps.Count - 1; i++)
            {
                point.X = road.Bps[i].ZhuangHao + road.T[i];

                point.Y = road.CalSjg(point.X);

                pts.Add(new DataPoint(point.X, point.Y));

                point.X = road.Bps[i + 1].ZhuangHao - road.T[i + 1];

                point.Y = road.CalSjg(point.X);

                pts.Add(new DataPoint(point.X, point.Y));

                int fd = (int)(road.T[i + 1] * 2 / 20);

                for (int j = 1; j <= fd + 1; j++)
                {
                    int tmpZH = ((int)(road.Bps[i + 1].ZhuangHao - road.T[i + 1])) / 20 * 20;

                    tmpZH = tmpZH + j * 20;

                    if (tmpZH >= (road.Bps[i + 1].ZhuangHao + road.T[i + 1]))
                    {
                        break;
                    }

                    //point.X = road.Bps[i + 1].ZhuangHao - road.T[i + 1] + j * 20;

                    point.X = tmpZH;

                    point.Y = road.CalSjg(point.X);

                    pts.Add(new DataPoint(point.X, point.Y));

                }

            }

            MainViewModel mainViewModel = this.DataContext as MainViewModel;

            if (mainViewModel == null)
            {
                return;
            }

            LineSeries series = mainViewModel.DynamicCurve.Series[0] as LineSeries;

            series?.Points.Clear();

            LineSeries dmxSeries = mainViewModel.DynamicCurve.Series[1] as LineSeries;
            
            dmxSeries?.Points.Clear();
            
            foreach (DataPoint pt in pts)
            {
                series.Points.Add(pt);

            }

            if (road.Dmx?.Count > 0)
            {

                if (dmxSeries != null)
                {
                    dmxSeries.Points.Clear();

                    for (int i = 0; i < road.Dmx.Count; i++)
                    {
                        double dmxZh = road.Dmx[i].DmxZhuangHao;

                        double dmxGao = road.Dmx[i].DmxGaoCheng;

                        dmxSeries.Points.Add(new DataPoint(dmxZh, dmxGao));

                    }
                }

            }

            mainViewModel.DynamicCurve.DefaultXAxis.Zoom(1);

            mainViewModel.DynamicCurve.DefaultYAxis.Zoom(10);

            mainViewModel.DynamicCurve.InvalidatePlot(true);
        }

        private void Button22_Click(object sender, RoutedEventArgs e)
        {
            double sjg = 0;
            double dmg = 0;
            double zh = 0;
            double calZh = 0;

            bool t_F = double.TryParse(Textbox3.Text, out zh);

            if (t_F == false || road is null||road.Bps.Count==0)
            {
                return;
            }

         

            if (road.Dl?.Count > 0)
            {
                LuDuan ld = new LuDuan();

                bool b = false;

                foreach (var item in road.Ld)
                {
                    string str = "";

                    if (item.Containzh(zh) && item.ToString().Contains(str))
                    {
                        b = true;

                        ld = item;

                        break;

                    }

                }

                if (b == false)
                {
                    Textbox4.Text = "桩号位于短链";

                    return;
                }

                //LuDuan ld = comboBox16.SelectedItem as LuDuan;

                if (ld != null)
                {
                    calZh = road.GetRealZh(zh, ld);
                }
                else
                {
                    calZh = zh;
                }

            }
            else
            {
                calZh = zh;
            }
            
            string _Gb = "";
            string _Laa = "";
            string _Lab = "";
            string _Lac = "";
            string _Lbb = "";
            string _Lbc = "";
            string _Lba = "";
            string _Lca = "";
            string _Lcb = "";
            string _Lcc = "";






            if (calZh < road.RealBps[0].ZhuangHao || calZh > road.RealBps[road.RealBps.Count - 1].ZhuangHao)
            {
                Textbox4.Text = "不在纵断数据范围";

                return;

            }
            else
            {

                sjg = road.CalSjg(calZh);

                Textbox29.Text = string.Empty;

                Textbox29.IsEnabled = false;

            }


            int i = 1;


            foreach (var item in road.GetRealZhs(zh))
            {
                double mul_sjg = road.CalSjg(item);

                Textbox29.Text = Textbox29.Text + "第" + i.ToString() + "个设计高为" + string.Format("{0:N4}", mul_sjg) + "\r\n";

                i++;
            }



            road.CalSS(calZh, out _Gb, out _Laa, out _Lab, out _Lac, out _Lbb, out _Lbc, out _Lba, out _Lca, out _Lcb, out _Lcc);

            Gb.Header = _Gb;
            laa.Content = _Laa;
            lab.Content = _Lab;
            lac.Content = _Lac;
            lba.Content = _Lba;
            lbb.Content = _Lbb;
            lbc.Content = _Lbc;
            lca.Content = _Lca;
            lcb.Content = _Lcb;
            lcc.Content = _Lcc;


            double tzValue = 0;

            //textBox2.Text是调整高程数值，textBox10.Text

            Textbox4.Text = Math.Round(sjg, 3).ToString();

            Textbox4.Text = Math.Round((decimal)sjg, 3, MidpointRounding.AwayFromZero).ToString();


            if (double.TryParse(Textbox2.Text, out tzValue))
            {
                Textbox10.Text = string.Format("{0:N4}", sjg + tzValue);

            }
            else
            {
                Textbox10.Text = string.Format("{0:N4}", sjg);

            }





            if (road.Dmx?.Count > 0)
            {
                dmg = Jsdmg(road.Dmx, calZh);

                Textbox11.Text = string.Format("{0:N4}", dmg);

                Textbox12.Text = string.Format("{0:N4}", sjg - dmg);

                //WriteToDgV(calZh, Math.Round(sjg, 3), Math.Round(dmg, 3), Math.Round(sjg, 3) - Math.Round(dmg, 3));

            }
            else
            {

                //WriteToDgV(calZh, Math.Round(sjg, 3));
            }







        }


        public double Jsdmg(List<DmxShuJu> dmxs, double zh)
        {
            double dmg = 0;

            for (int i = 0; i < dmxs.Count - 1; i++)
            {
                if (zh >= dmxs[i].DmxZhuangHao && zh <= dmxs[i + 1].DmxZhuangHao)
                {
                    dmg = dmxs[i].DmxGaoCheng + (zh - dmxs[i].DmxZhuangHao) * (dmxs[i + 1].DmxGaoCheng - dmxs[i].DmxGaoCheng) / (dmxs[i + 1].DmxZhuangHao - dmxs[i].DmxZhuangHao);
                }


            }

            if (zh < dmxs[0].DmxZhuangHao)
            {
                dmg = dmxs[0].DmxGaoCheng;
            }
            if (zh >= dmxs[dmxs.Count - 1].DmxZhuangHao)
            {
                dmg = dmxs[dmxs.Count - 1].DmxGaoCheng;
            }


            return dmg;

        }

        private void Button10_Click(object sender, RoutedEventArgs e)
        {
            double zhjj = 0;

            double jszh = 0;


            if (double.TryParse(Textbox3.Text, out jszh))
            {

                if (double.TryParse(Textbox5.Text, out zhjj))
                {
                    Textbox3.Text = (jszh - zhjj).ToString();

                }

            }

            Button22_Click(sender, e);


        }
        /// <summary>
        /// 计算后一个桩号，程序稳定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, RoutedEventArgs e)
        {
            double zhjj = 0;

            double jszh = 0;


            if (double.TryParse(Textbox3.Text, out jszh))
            {

                if (double.TryParse(Textbox5.Text, out zhjj))
                {
                    Textbox3.Text = (jszh + zhjj).ToString();

                }

            }

            Button22_Click(sender, e);
        }

        private void Button6_Click(object sender, RoutedEventArgs e)
        {

            if (road?.RealBps.Count>1)
            {

                List<double> z = new List<double>();

                for (int i = 1; i < road?.Zp.Count - 1; i++)
                {
                    z.Add(Math.Abs(road.Zp[i]));
                }

                comboBox1.Items.Clear();

                for (int i = 1; i < road?.Zp.Count - 1; i++)
                {
                    comboBox1.Items.Add(string.Format("{0:P3}", road.Zp[i]));
                }

                if (comboBox1.Items.Count > 0)
                {
                    comboBox1.SelectedIndex = 0;
                }

                //double maxZp = Max(z);

                double maxZp = z.Max();

                int js = 0;

                js = z.Where(tmpP => tmpP == maxZp).Count();


                TextBox6.Text = string.Format("{0:P2}", maxZp) + "-" + js.ToString();


                //double minZp = Min(z);

                double minZp = z.Min();

                js = z.Where(tmpP => tmpP == minZp).Count();


                TextBox7.Text = string.Format("{0:P2}", minZp) + "-" + js.ToString();

                List<double> poChang = new List<double>();

                poChang = road.ZpsLenth;

                //for (int i = 0; i < bps.Count - 1; i++)
                //{
                //    poChang.Add(bps[i + 1].ZhuangHao - bps[i].ZhuangHao);

                //}

                for (int i = 0; i < poChang.Count; i++)
                {
                    comboBox2.Items.Add(string.Format("{0:0.###}", poChang[i]));

                }
                if (comboBox2.Items.Count > 0)
                {
                    comboBox2.SelectedIndex = 0;
                }

                double minPoChang = poChang.Min();


                js = 0;
                js = poChang.Where(p => p == minPoChang).Count();

         

                TextBox15.Text = string.Format("{0:0.###}", minPoChang) + "-" + js.ToString();

                List<double> Zpc = new List<double>();

                for (int i = 0; i < poChang.Count; i++)
                {

                    Zpc.Add(poChang[i] - road.T[i] - road.T[i + 1]);



                }

                comboBox3.Items.Clear();

                for (int i = 0; i < Zpc.Count; i++)
                {

                    comboBox3.Items.Add(string.Format("{0:0.###}", Zpc[i]));

                }

                if (comboBox3.Items.Count > 0)
                {
                    comboBox3.SelectedIndex = 0;
                }

                double minZpc = Zpc.Min();

                js = Zpc.Where(zpc => zpc == minZpc).Count();

                TextBox16.Text = string.Format("{0:0.###}", minZpc) + "-" + js.ToString();

                double maxZpc = Zpc.Max();

                //A T 分别代表 凹 凸  竖曲线
                List<double> sqxT = new List<double>();

                List<double> sqxA = new List<double>();

                for (int i = 1; i < road.W.Count - 1; i++)
                {
                    if (road.W[i] > 0)
                    {
                        sqxA.Add(road.RealBps[i].ShuQuXian);

                    }
                    else
                    {
                        sqxT.Add(road.RealBps[i].ShuQuXian);
                    }

                }

                if (sqxA.Count > 0)
                {
                    double minAqx = sqxA.Min();

                    js = sqxA.Where(sqx => sqx == minAqx).Count();

                    TextBox18.Text = string.Format("{0:0.###}", minAqx) + "-" + js.ToString();

                }


                if (sqxT.Count > 0)
                {
                    double minTqx = sqxT.Min();

                    js = sqxT.Where(sqx => sqx == minTqx).Count();

                    TextBox17.Text = string.Format("{0:0.###}", minTqx) + "-" + js.ToString();

                }



                comboBox5.Items.Clear();

                for (int i = 0; i < sqxA.Count; i++)
                {
                    comboBox5.Items.Add(sqxA[i]);

                }

                if (comboBox5.Items.Count > 0)
                {
                    comboBox5.SelectedIndex = 0;
                }

                comboBox4.Items.Clear();

                for (int i = 0; i < sqxT.Count; i++)
                {
                    comboBox4.Items.Add(sqxT[i]);

                }

                if (comboBox4.Items.Count > 0)
                {
                    comboBox4.SelectedIndex = 0;
                }

                List<double> TT = new List<double>();

                foreach (var item in road.T)
                {
                    if (item != 0)
                    {
                        TT.Add(item);
                    }
                }

                double minSqxCd = TT.Min() * 2;


                js = TT.Where(t => t == minSqxCd / 2).Count();
                

                TextBox24.Text = string.Format("{0:0.###}", minSqxCd) + "-" + js.ToString();


                comboBox8.Items.Clear();

                for (int i = 0; i < road.T.Count; i++)
                {
                    comboBox8.Items.Add((road.T[i] * 2).ToString("0.###") + "--" + road.T[i].ToString("0.###"));
                }

                if (comboBox8.Items.Count > 0)
                {
                    comboBox8.SelectedIndex = 0;
                }


                //MessageBox.Show(road.W.Count.ToString());

                //MessageBox.Show(road.Zp.Count.ToString());

                //MessageBox.Show(road.RealBps.Count.ToString());

                //MessageBox.Show(road.W[0].ToString() + "-----" + road.W[1].ToString());


                List<double> zGd = new List<double>();

                List<double> zDd = new List<double>();


                comboBox15.Items.Clear();

                comboBox14.Items.Clear();

                for (int i = 0; i < road.W.Count; i++)
                {

                    //MessageBox.Show(road.W[i].ToString());


                    //如果W小于0说明是凸曲线，如果前坡大于0后坡小于0，有极大值
                    if (road.W[i] < 0 && road.Zp[i] >= 0 && road.Zp[i + 1] <= 0)
                    {

                        double x = Math.Abs(road.Zp[i] * road.RealBps[i].ShuQuXian);

                        x = road.RealBps[i].ZhuangHao - road.T[i] + x;

                        //MessageBox.Show(x.ToString());

                        double y = road.CalSjg(x);

                        zGd.Add(y);

                        comboBox15.Items.Add(Math.Round(x, 3).ToString() + "---" + Math.Round(y, 3).ToString());

                        //MessageBox.Show(x.ToString()+"-----"+y.ToString());

                    }

                    //如果W大于0说明是凹曲线，如果前坡小于0后坡大于0，有极小值
                    if (road.W[i] > 0 && road.Zp[i] <= 0 && road.Zp[i + 1] >= 0)
                    {

                        double x = Math.Abs(road.Zp[i] * road.RealBps[i].ShuQuXian);

                        x = road.RealBps[i].ZhuangHao - road.T[i] + x;

                        double y = road.CalSjg(x);

                        zDd.Add(y);

                        comboBox14.Items.Add(Math.Round(x, 3).ToString() + "---" + Math.Round(y, 3).ToString());

                        //MessageBox.Show(x.ToString() + "-----" + y.ToString());

                    }

                }
                if (zDd.Count > 0)
                {
                    double minzDd = Math.Round(Min(zDd), 3);
                    
                    textBox33.Text = minzDd.ToString();
                }

                if (zGd.Count>0)
                {
                    double maxzGd = Math.Round(Max(zGd), 3);

                    textBox32.Text = maxzGd.ToString();
                }



                if (comboBox15.Items.Count > 0)
                {
                    comboBox15.SelectedIndex = 0;
                }


                if (comboBox14.Items.Count > 0)
                {
                    comboBox14.SelectedIndex = 0;
                }





            }



        }

        public static double Max(List<double> dbs)
        {
            if (dbs == null) throw new Exception("数组空异常");
            double value = 0;
            bool hasValue = false;
            foreach (double x in dbs)
            {
                if (hasValue)
                {
                    if (x > value)
                        value = x;
                }
                else
                {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw new Exception("没找到");
        }

        public static double Min(List<double> dbS)
        {
            if (dbS == null) throw new Exception("数组空异常");
            double value = 0;
            bool hasValue = false;
            foreach (double x in dbS)
            {
                if (hasValue)
                {
                    if (x < value) value = x;
                }
                else
                {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw new Exception("没找到");
        }

        public static double Pj(List<double> dbS)
        {
            double av = 0;

            if (dbS == null) throw new Exception("数组空异常");

            double sum = 0;

            foreach (double item in dbS)
            {
                sum = sum + item;
            }

            if (dbS.Count > 0)
            {
                av = sum / dbS.Count;
            }


            return av;
        }

        private void Button1111_Click(object sender, RoutedEventArgs e)
        {

            double zhjj = 0;

            bool b_jj = double.TryParse(textBox34.Text, out zhjj);

            if (b_jj == false)
            {
                return;
            }

            double tmpQdZh = 0;

            double tmpZdZh = 0;

            bool t_F = double.TryParse(TextBox40.Text, out tmpQdZh);

            if (t_F == false)
            {
                return;
            }


            t_F = double.TryParse(TextBox41.Text, out tmpZdZh);

            if (t_F == false)
            {
                return;
            }

            double qdzh = 0;

            double zdzh = 0;



            if (road.Dl.Count > 0)
            {

                LuDuan lda = new LuDuan();

                LuDuan ldb = new LuDuan();

                string strA = comboBox10.Text;

                string strB = comboBox11.Text;


                foreach (var item in road.Ld)
                {

                    if (item.Containzh(tmpQdZh) && item.ToString().Contains(strA))
                    {
                        lda = item;

                    }
                    if (item.Containzh(tmpZdZh) && item.ToString().Contains(strB))
                    {
                        ldb = item;

                    }

                }


                if (lda != null)
                {
                    qdzh = road.GetRealZh(tmpQdZh, lda);

                    //MessageBox.Show(qdzh.ToString());
                }


                if (ldb != null)
                {
                    zdzh = road.GetRealZh(tmpZdZh, ldb);

                    //MessageBox.Show(zdzh.ToString());
                }
            }
            else
            {
                qdzh = tmpQdZh;

                zdzh = tmpZdZh;
            }


            if (qdzh >= zdzh)
            {

                TextBox13.Text = "起点桩号＞终点桩号";

                return;
            }



            double calZh = 0;

            List<double> sjgS = new List<double>();

            int i = 0;


            calZh = qdzh;

            while (calZh <= zdzh)
            {
                double sjg = road.CalSjg(calZh);

                sjgS.Add(sjg);

                i++;

                calZh = qdzh + i * zhjj;

                //double dmg = 0;

                //if (road?.Dmx.Count>0)
                //{
                //    dmg = Jsdmg(dmxs, calZh);

                //    //WriteToDgV(calZh, Math.Round(sjg, 3), Math.Round(dmg, 3), Math.Round(sjg, 3) - Math.Round(dmg, 3));

                //}
                //else
                //{

                //    //WriteToDgV(calZh, Math.Round(sjg, 3));
                //}

            }




            double sum = 0;

            foreach (double item in sjgS)
            {
                sum = sum + item;

            }

            double pjSjg = sum / sjgS.Count;

            TextBox13.Text = string.Format("{0:0.###}", pjSjg);



        }

        private void TextBox40_TextChanged(object sender, TextChangedEventArgs e)
        {
            double zh = 0;

            bool t_F = double.TryParse(TextBox40.Text, out zh);

            if (t_F != true)
            {
                return;
            }

            comboBox10.Items.Clear();

            if (road.Dl.Count > 0)
            {
                bool b = false;

                foreach (var item in road.Ld)
                {

                    if (item.Containzh(zh) && comboBox10.Items.Contains(item.ToString()) == false)
                    {
                        comboBox10.IsEnabled = true;

                        comboBox10.Items.Add(item.ToString());

                        b = true;
                    }

                }

                if (b)
                {

                    comboBox10.SelectedIndex = 0;

                }
                else
                {
                    comboBox10.Items.Clear();

                    comboBox10.Items.Add("位于短链范围");

                    comboBox10.SelectedIndex = 0;

                    comboBox10.IsEnabled = false;
                }

            }
            else
            {
                comboBox10.Items.Clear();

                comboBox10.Items.Add("没有断链");

                comboBox10.SelectedIndex = 0;

                comboBox10.IsEnabled = false;


            }
        }

        private void TextBox41_TextChanged(object sender, TextChangedEventArgs e)
        {
            double zh = 0;

            bool t_F = double.TryParse(TextBox41.Text, out zh);

            if (t_F != true)
            {
                return;
            }

            comboBox11.Items.Clear();

            if (road.Dl.Count > 0)
            {


                bool b = false;

                foreach (var item in road.Ld)
                {

                    if (item.Containzh(zh) && comboBox11.Items.Contains(item.ToString()) == false)
                    {
                        comboBox11.IsEnabled = true;

                        comboBox11.Items.Add(item.ToString());

                        b = true;
                    }

                }

                if (b)
                {

                    comboBox11.SelectedIndex = 0;

                }
                else
                {
                    comboBox11.Items.Clear();

                    comboBox11.Items.Add("位于短链范围");

                    comboBox11.SelectedIndex = 0;

                    comboBox11.IsEnabled = false;
                }

            }
            else
            {

                comboBox11.Items.Clear();

                comboBox11.Items.Add("没有断链");

                comboBox11.SelectedIndex = 0;

                comboBox11.IsEnabled = false;


            }






        }

        private void Button11_Click(object sender, RoutedEventArgs e)
        {
            if (road?.RealBps?.Count>1)
            {

                List<double> zhFw = new List<double>();

                zhFw.Clear();

                double qdzh = 0;

                double zdzh = 0;

                double zhjj = 0;

                double zhCal = 0;

                double gdZp = 0;

                bool b = double.TryParse(textBox22.Text, out gdZp);

                bool b_qd = double.TryParse(textBox8.Text, out qdzh);

                bool b_zd = double.TryParse(textBox9.Text, out zdzh);

                bool b_jj = double.TryParse(textBox21.Text, out zhjj);

                bool hasQd = false;

                gdZp = gdZp / 100;

                if (b && b_qd && b_zd && b_jj)
                {

                    if (road.Dl.Count > 0)
                    {

                        LuDuan lda = new LuDuan();

                        LuDuan ldb = new LuDuan();

                        string strA = comboBox13.Text;

                        string strB = comboBox12.Text;


                        foreach (var item in road.Ld)
                        {

                            if (item.Containzh(qdzh) && item.ToString().Contains(strA))
                            {
                                lda = item;

                            }
                            if (item.Containzh(zdzh) && item.ToString().Contains(strB))
                            {
                                ldb = item;

                            }

                        }

                        if (lda != null)
                        {
                            qdzh = road.GetRealZh(qdzh, lda);

                            //MessageBox.Show(qdzh.ToString());

                        }

                        if (ldb != null)
                        {
                            zdzh = road.GetRealZh(zdzh, ldb);

                            //MessageBox.Show(zdzh.ToString());

                        }

                    }


                    zhCal = qdzh;

                    while (zhCal < zdzh)
                    {

                        double zp = road.CalSsZp(zhCal);

                        if (hasQd == false)
                        {

                            if (Math.Abs(zp) >= gdZp)
                            {
                                zhFw.Add(zhCal);

                                hasQd = true;
                            }

                        }
                        else
                        {
                            if (Math.Abs(zp) <= gdZp)
                            {
                                zhFw.Add(zhCal);

                                hasQd = false;
                            }

                        }

                        zhCal = zhCal + zhjj;


                    }


                    if (zhFw.Count % 2 == 1)
                    {
                        zhFw.Add(zdzh);

                    }

                }


                double sum = 0;

                for (int i = 0; i < zhFw.Count; i++)
                {
                    double tmpL = zhFw[i + 1] - zhFw[i];

                    sum = sum + tmpL;

                    i++;

                }

                textBox23.Text = string.Format("{0:0.###}", sum);

                comboBox7.Items.Clear();

                for (int i = 0; i < zhFw.Count; i++)
                {
                    comboBox7.Items.Add(zhFw[i].ToString("0.###") + "--" + zhFw[i + 1].ToString("0.###"));

                    i++;

                }

                if (comboBox7.Items.Count > 0)
                {
                    comboBox7.SelectedIndex = 0;
                }
            }


        }

        private void Button33_Click(object sender, RoutedEventArgs e)
        {
            if (road?.RealBps.Count>1)
            {


                List<double> zhFw = new List<double>();

                double qdzh = 0;

                double zdzh = 0;

                double zhjj = 0;

                double calZh = 0;

                double hyZp = 0;

                bool b = double.TryParse(textBox19.Text, out hyZp);

                bool b_qd = double.TryParse(textBox8.Text, out qdzh);

                bool b_zd = double.TryParse(textBox9.Text, out zdzh);

                bool b_jj = double.TryParse(textBox21.Text, out zhjj);

                bool hasQd = false;

                hyZp = hyZp / 100;

                if (b && b_qd && b_zd && b_jj)
                {

                    if (road.Dl.Count > 0)
                    {

                        LuDuan lda = new LuDuan();

                        LuDuan ldb = new LuDuan();

                        string strA = comboBox13.Text;

                        string strB = comboBox12.Text;


                        foreach (var item in road.Ld)
                        {

                            if (item.Containzh(qdzh) && item.ToString().Contains(strA))
                            {
                                lda = item;

                            }
                            if (item.Containzh(zdzh) && item.ToString().Contains(strB))
                            {
                                ldb = item;

                            }

                        }

                        if (lda != null)
                        {
                            qdzh = road.GetRealZh(qdzh, lda);

                            //MessageBox.Show(qdzh.ToString());

                        }

                        if (ldb != null)
                        {
                            zdzh = road.GetRealZh(zdzh, ldb);

                            //MessageBox.Show(zdzh.ToString());

                        }

                    }


                    //输入的起终点桩号需要换算

                    calZh = qdzh;

                    while (calZh < zdzh)
                    {

                        double zp = road.CalSsZp(calZh);

                        if (hasQd == false)
                        {

                            if (Math.Abs(zp) <= hyZp)
                            {
                                zhFw.Add(calZh);

                                hasQd = true;
                            }

                        }
                        else
                        {
                            if (Math.Abs(zp) >= hyZp)
                            {
                                zhFw.Add(calZh);

                                hasQd = false;
                            }

                        }

                        calZh = calZh + zhjj;

                    }


                    if (zhFw.Count % 2 == 1)
                    {
                        zhFw.Add(zdzh);

                    }


                }

                double sum = 0;

                for (int i = 0; i < zhFw.Count; i++)
                {
                    double tmpL = zhFw[i + 1] - zhFw[i];

                    sum = sum + tmpL;

                    i++;

                }

                textBox20.Text = string.Format("{0:0.###}", sum);

                comboBox6.Items.Clear();

                for (int i = 0; i < zhFw.Count; i++)
                {
                    comboBox6.Items.Add(zhFw[i].ToString("0.###") + "--" + zhFw[i + 1].ToString("0.###"));

                    i++;

                }


                if (comboBox6.Items.Count > 0)
                {
                    comboBox6.SelectedIndex = 0;
                }

            }


        }

        private void Button12_Click(object sender, RoutedEventArgs e)
        {
            Button33_Click(sender, e);

            Button11_Click(sender, e);
        }

        private void TextBox8_TextChanged(object sender, TextChangedEventArgs e)
        {
            double zh = 0;

            bool t_F = double.TryParse(textBox8.Text, out zh);

            if (t_F != true)
            {
                return;
            }
            if (comboBox13 is null)
            {
                return;
            }


            comboBox13.Items.Clear();

            if (road.Dl.Count > 0)
            {
                bool b = false;

                foreach (var item in road.Ld)
                {

                    if (item.Containzh(zh) && comboBox13.Items.Contains(item.ToString()) == false)
                    {
                        comboBox13.IsEnabled = true;

                        comboBox13.Items.Add(item.ToString());

                        b = true;
                    }

                }

                if (b)
                {

                    comboBox13.SelectedIndex = 0;

                }
                else
                {
                    comboBox13.Items.Clear();

                    comboBox13.Items.Add("位于短链范围");

                    comboBox13.SelectedIndex = 0;

                    comboBox13.IsEnabled = false;
                }

            }
            else
            {
                comboBox13.Items.Clear();

                comboBox13.Items.Add("没有断链");

                comboBox13.SelectedIndex = 0;

                comboBox13.IsEnabled = false;


            }
        }

        private void TextBox9_TextChanged(object sender, TextChangedEventArgs e)
        {
            double zh = 0;

            bool t_F = double.TryParse(textBox9.Text, out zh);

            if (t_F != true)
            {
                return;
            }
            if (comboBox12 is null)
            {
                return;
            }
            comboBox12.Items.Clear();

            if (road.Dl.Count > 0)
            {
                bool b = false;

                foreach (var item in road.Ld)
                {

                    if (item.Containzh(zh) && comboBox12.Items.Contains(item.ToString()) == false)
                    {
                        comboBox12.IsEnabled = true;

                        comboBox12.Items.Add(item.ToString());

                        b = true;
                    }

                }

                if (b)
                {

                    comboBox12.SelectedIndex = 0;

                }
                else
                {
                    comboBox12.Items.Clear();

                    comboBox12.Items.Add("位于短链范围");

                    comboBox12.SelectedIndex = 0;

                    comboBox12.IsEnabled = false;
                }

            }
            else
            {
                comboBox12.Items.Clear();

                comboBox12.Items.Add("没有断链");

                comboBox12.SelectedIndex = 0;

                comboBox12.IsEnabled = false;

            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainViewModel mainViewModel = this.DataContext as MainViewModel;

            if (mainViewModel==null)
            {
                return;
            }
            
            LineSeries series = mainViewModel.DynamicCurve.Series[0] as LineSeries;

            series.Points.Add(new DataPoint(0, 0));

            series.Points.Add(new DataPoint(100,100));

            mainViewModel.DynamicCurve.InvalidatePlot(true);

            //this.DataContext = mainViewModel;
            
        }

        private void PlotView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
           ( (MainViewModel)this.DataContext).DynamicCurve.InvalidatePlot(true);
        }

        private void Button3_Click(object sender, RoutedEventArgs e)
        {

            List<BianPoShuJu> bps = new List<BianPoShuJu>();

            road.Bps.Clear();

            road.RealBps.Clear();
            
            ExcelData e_data = new ExcelData();

            MessageBox.Show("切换到excel复制竖曲线后点击确认，要求为三列！");
            
            try
            {

                string[,] my_e_data = e_data.GetExcelPaste();

                string[] tm_th = new string[my_e_data.GetLongLength(0)];

                for (int i = 0; i < my_e_data.GetLongLength(0); i++)
                {
                    double tm_tmpZh = 0;
                    double tm_tmpGao = 0;
                    double tm_tmpSqx = 0;

                    bool a = double.TryParse(my_e_data[i, 0], out tm_tmpZh);

                    bool b = double.TryParse(my_e_data[i, 1], out tm_tmpGao);

                    bool c = double.TryParse(my_e_data[i, 2], out tm_tmpSqx);

                    BianPoShuJu bp = new BianPoShuJu(tm_tmpZh, tm_tmpGao, tm_tmpSqx);

                    bps.Add(bp);

                }

            }
            catch (System.Exception)
            {

                MessageBox.Show("似乎没有按照做哦，再复制一下吧，要选三列哦！");

                return;

                //throw;
            }

            try
            {
                road.Bps = bps;
                
            }
            catch (Exception)
            {
                return;
            }

            if (road.Bps?.Count > 0)
            {
                DGVA.ItemsSource = road.Bps;

                Textbox3.Text = road.MinZdmZh.ToString();
            }

            if (road.Dmx?.Count > 0)
            {
                DGVB.ItemsSource = road.Dmx;
            }

            Button22_Click(sender, e);

            //TextBox1  平均计算高的起点桩号

            TextBox40.Text = road.Bps[0].ZhuangHao.ToString();

            //TextBox14  平均计算高的终点桩号

            TextBox41.Text = road.Bps[road.Bps.Count - 1].ZhuangHao.ToString();

            //陡缓查询的起终点范围

            textBox8.Text = road.Bps[0].ZhuangHao.ToString();

            textBox9.Text = road.Bps[road.Bps.Count - 1].ZhuangHao.ToString();

            //处理绘图部分

            DrawCurve();
        }

        private void Button1_Click_1(object sender, RoutedEventArgs e)
        {
            List<DmxShuJu> dmxSj = new List<DmxShuJu>();

            ExcelData e_data = new ExcelData();

            MessageBox.Show("切换到excel复制竖曲线后点击确认，要求为< 两列 >！");

            try
            {

                string[,] my_e_data = e_data.GetExcelPaste();

                string[] tm_th = new string[my_e_data.GetLongLength(0)];

                for (int i = 0; i < my_e_data.GetLongLength(0); i++)
                {
                    double tm_tmpZh = 0;
                    double tm_tmpGao = 0;
                
                    bool a = double.TryParse(my_e_data[i, 0], out tm_tmpZh);

                    bool b = double.TryParse(my_e_data[i, 1], out tm_tmpGao);
                    
                    DmxShuJu dmx = new DmxShuJu(tm_tmpZh, tm_tmpGao);

                    dmxSj.Add(dmx);

                }

            }
            catch (System.Exception)
            {

                MessageBox.Show("似乎没有按照做哦，再复制一下吧，要选 两列 哦！");

                return;

                //throw;
            }

            try
            {
                road.Dmx = dmxSj;

            }
            catch (Exception)
            {
                return;
            }

            if (road.Dmx?.Count > 0)
            {
                DGVB.ItemsSource = road.Dmx;
            }

            //处理绘图部分

            DrawCurve();
        }
    }


    public class Road
    {
        private string dataFilePath = string.Empty;

        private bool hasDl = false;

        public Road()
        {




        }
        public Road(string _dataFilePath)
        {
            dataFilePath = _dataFilePath;

            List<Duanlian> dls = new List<Duanlian>();

            HasDl = DuRuDlShuJu(dataFilePath, out dls);

            if (HasDl)
            {
                this.Dl = dls;
            }

            bool hasZdm = false;
            List<BianPoShuJu> _bps = new List<BianPoShuJu>();
            List<string> _bpsStr = new List<string>();
            hasZdm = DuRuZdmShuJu(dataFilePath, out _bps, out _bpsStr);

            if (hasZdm)
            {
                this.Bps = _bps;
                this.BpdsStr = _bpsStr;
                this.minZdmZh = _bps[0].ZhuangHao;
                
                
            }
            bool hasDmx = false;

            List<DmxShuJu> _dmx = new List<DmxShuJu>();

            hasDmx = DuRuDmxShuJu(dataFilePath, out _dmx);

            if (hasDmx)
            {
                this.Dmx = _dmx;
            }
            
        }

        public bool DuRuDlShuJu(string _fileName, out List<Duanlian> dls)
        {
            bool b = false;

            dls = new List<Duanlian>();

            StreamReader MstreamReader = new StreamReader(_fileName, System.Text.Encoding.Default);

            string Lstr = null;

            int BiaoShiFu = 0;

            Lstr = MstreamReader.ReadLine();

            while (Lstr != null)
            {
 
                if (Lstr.Trim().Length < 3)
                {
                    try
                    {
                        BiaoShiFu = Convert.ToInt16(Lstr.Trim());

                        if (BiaoShiFu == 1)
                        {

                            Lstr = MstreamReader.ReadLine();

                            do
                            {
                                char[] c = { '|' };

                                Lstr = Lstr.Trim(c);

                                Duanlian dl = new Duanlian(Lstr);

                                dls.Add(dl);


                                Lstr = MstreamReader.ReadLine();

                            }

                            while (Lstr.Trim().Contains("|"));

                        }

                    }
                    catch
                    {

                    }
                    finally
                    {

                    }

                }

                Lstr = MstreamReader.ReadLine();

            }

            MstreamReader.Close();


            if (dls.Count > 0)
            {
                b = true;
            }

            return b;
        }

        public bool DuRuZdmShuJu(string _fileName, out List<BianPoShuJu> _bps, out List<string> _bpsStr)
        {
            bool b = false;

            _bpsStr = new List<string>();

            _bps = new List<BianPoShuJu>();


            StreamReader MstreamReader = new StreamReader(_fileName, System.Text.Encoding.Default);

            string Lstr = null;

            int BiaoShiFu = 0;

            Lstr = MstreamReader.ReadLine();

            while (Lstr != null)
            {

                if (Lstr.Trim().Length < 3)
                {
                    try
                    {
                        BiaoShiFu = Convert.ToInt16(Lstr.Trim());

                        if (BiaoShiFu == 4)
                        {
                            Lstr = MstreamReader.ReadLine();
                            do
                            {
                                char[] c = { '|' };
                                Lstr = Lstr.Trim(c);
                                BianPoShuJu bpd = new BianPoShuJu(Lstr);
                                _bpsStr.Add(Lstr);
                                _bps.Add(bpd);
                                Lstr = MstreamReader.ReadLine();
                            }
                            while (Lstr.Trim().Contains("|"));
                        }
                    }
                    catch
                    {

                    }
                    finally
                    {

                    }

                }

                Lstr = MstreamReader.ReadLine();

            }

            MstreamReader.Close();

            if (_bps.Count>0)
            {
                b = true;
            }
            
            return b;
        }

        public bool DuRuDmxShuJu(string _fileName, out List<DmxShuJu> dmxs)
        {
            bool hasDmx = false;

            dmxs = new List<DmxShuJu>();

            if (File.Exists(_fileName))
            {
                StreamReader MstreamReader = new StreamReader(_fileName, System.Text.Encoding.Default);

                string Lstr = null;

                int BiaoShiFu = 0;

                Lstr = MstreamReader.ReadLine();

                while (Lstr != null)
                {
                    //this.TextBox1.AppendText(Lstr);

                    if (Lstr.Trim().Length < 3)
                    {
                        try
                        {
                            BiaoShiFu = Convert.ToInt16(Lstr.Trim());

                            if (BiaoShiFu == 3)
                            {

                                //MessageBox.Show(BiaoShiFu.ToString ());

                                Lstr = MstreamReader.ReadLine();

                                do
                                {
                                    char[] c = { '|' };

                                    Lstr = Lstr.Trim(c);

                                    DmxShuJu dmx = new DmxShuJu(Lstr);

                                    dmxs.Add(dmx);


                                    Lstr = MstreamReader.ReadLine();

                                }

                                while (Lstr.Trim().Contains("|"));

                            }

                        }
                        catch
                        {

                        }
                        finally
                        {

                        }

                    }

                    Lstr = MstreamReader.ReadLine();

                }

                MstreamReader.Close();

            }

            else
            {

            }

            if (dmxs.Count > 0)
            {
                hasDmx = true;

            }

            return hasDmx;

        }
        
        private double minZdmZh = 0;

        public double MinZdmZh
        {
            get { return minZdmZh; }
            //set { minZdmZh = value; }
        }

        private double maxZdZh = 0;

        public double MaxZdZh
        {
            get { return maxZdZh; }
            set { maxZdZh = value; }
        }

        List<string> bpdsStr = new List<string>();

        public List<string> BpdsStr
        {
            get { return bpdsStr; }
            set { bpdsStr = value; }
        }


        private List<double> zp = new List<double>();

        public List<double> Zp
        {
            get { return zp; }
            set { zp = value; }
        }

        private List<double> w = new List<double>();

        public List<double> W
        {
            get { return w; }
            set { w = value; }
        }

        private List<double> t = new List<double>();

        public List<double> T
        {
            get { return t; }
            set { t = value; }
        }



        private List<LuDuan> ld = new List<LuDuan>();

        public List<LuDuan> Ld
        {
            get { return ld; }
            //set { ld = value; }
        }

        private List<Duanlian> dl = new List<Duanlian>();

        //设置完断链，自动依据断链位置把路线桩号划分为几个段落
        public List<Duanlian> Dl
        {
            get { return dl; }
            set
            {

                dl = value;

                LuDuan A = new LuDuan();

                for (int i = 0; i < dl.Count; i++)
                {

                    if (i == 0)
                    {
                        A = new LuDuan(dl[i].DlqZh - 10000000, dl[i].DlqZh);
                    }

                    else
                    {

                        A = new LuDuan(dl[i - 1].DlhZh, dl[i].DlqZh);

                    }

                    ld.Add(A);

                }

                A = new LuDuan(dl[dl.Count - 1].DlhZh, dl[dl.Count - 1].DlhZh + 100000000);

                ld.Add(A);

            }
        }

        private List<double> zpsLenth = new List<double>();

        public List<double> ZpsLenth
        {
            get { return zpsLenth; }
            set { zpsLenth = value; }
        }
        
        List<DmxShuJu> dmxs = new List<DmxShuJu>();

        public List<DmxShuJu> Dmx
        {
            get { return dmxs; }
            set { dmxs = value; }
        }

        List<BianPoShuJu> bps = new List<BianPoShuJu>();

        public List<BianPoShuJu> Bps
        {
            get { return bps; }
            set
            {

                bps = value;

                //把每个桩号对应到相应的段落

                for (int i = 0; i < bps.Count; i++)
                {
                    for (int j = 0; j < this.Ld.Count; j++)
                    {

                        if (this.Ld[j].Containzh(bps[i].ZhuangHao))
                        {

                            bps[i].DlNo = j + 1;
                            
                            break;

                        }

                    }

                }

                //所有包含断链的桩号信息，调整为没有断链的真实竖曲线数据


                int n = bps[0].DlNo;

                int m = 0;

                double tmpRealZh = 0;

                for (int i = 0; i < bps.Count; i++)
                {

                    m = bps[i].DlNo;

                    double tzZh = 0;

                    //if (i > 0)
                    //{
                        for (int j = 0; j < m - 1; j++)
                        {

                            if (dl[j].IsLong)
                            {
                                tzZh = tzZh + dl[j].AbsLong;
                            }

                            if (dl[j].IsShort)
                            {
                                tzZh = tzZh - dl[j].AbsLong;
                            }

                        }

                    //}

                    tmpRealZh = bps[i].ZhuangHao + tzZh;

                    BianPoShuJu tmpBp = new BianPoShuJu(tmpRealZh, bps[i].BpdGao, bps[i].ShuQuXian, bps[i].DlNo);

                    realBps.Add(tmpBp);

                }

                //根据真实的竖曲线数据初始化 zp w t 

                //minZdmZh = bps[0].ZhuangHao;

                //maxZdZh = bps[bps.Count - 1].ZhuangHao;

                zp = new List<double>();

                w = new List<double>();

                t = new List<double>();

                try
                {

                    int nn = realBps.Count;

                    int xsws = 6;

                    zp.Clear();

                    double l = 0;

                    double h = 0;

                    h = realBps[1].BpdGao - realBps[0].BpdGao;

                    //先计算实际纵断的第一个坡度

                    l = realBps[1].ZhuangHao - realBps[0].ZhuangHao;

                    zp.Add(h / l);

                    //zp.Add(Math.Round((h / l), xsws));


                    //计算纵断面数据中的实际的坡长与坡度；

                    for (int i = 1; i < nn; i++)
                    {

                        l = realBps[i].ZhuangHao - realBps[i - 1].ZhuangHao;

                        zpsLenth.Add(l);

                        h = realBps[i].BpdGao - realBps[i - 1].BpdGao;

                        //zp.Add(Math.Round((h / l), xsws));

                        zp.Add(h / l);


                    }

                    l = realBps[realBps.Count - 1].ZhuangHao - realBps[realBps.Count - 2].ZhuangHao;

                    h = realBps[realBps.Count - 1].BpdGao - realBps[realBps.Count - 2].BpdGao;

                    zp.Add(h / l);

                    //zp.Add(Math.Round((h / l), xsws));

                    //纵坡数据全部计算完成，并去除了断链的影响

                    for (int i = 0; i < nn; i++)
                    {
                        w.Add(zp[i + 1] - zp[i]);
                    }

                    for (int i = 0; i < nn; i++)
                    {
                        t.Add(Math.Abs(w[i] * realBps[i].ShuQuXian / 2));

                    }

                }
                catch (Exception e)
                {

                    MessageBox.Show(e.Message.ToString() + "w我出错了");
                    zp = null;
                    t = null;
                    w = null;

                }

            }
        }


        List<BianPoShuJu> realBps = new List<BianPoShuJu>();

        public List<BianPoShuJu> RealBps
        {
            get { return realBps; }
            //set { realBps = value; }
        }

        public bool HasDl { get => hasDl; set => hasDl = value; }












        #region  方法定义

        /// <summary>
        /// 计算桩号处的设计高
        /// </summary>
        /// <param name="zh"></param>
        /// <returns></returns>
        public double CalSjg(double zh)
        {
            //zh 需要换算为可能的zh

            //List<double> sjgs = new List<double>();

            double hsQdZh = 0;

            double hsZdZh = 0;

            double h = 0;

            double tmp_sjg = 0;


            for (int i = 0; i <= realBps.Count - 2; i++)
            {
                hsQdZh = realBps[i].ZhuangHao;

                hsZdZh = realBps[i + 1].ZhuangHao;

                h = realBps[i].BpdGao;


                if (zh >= (hsQdZh + t[i]) & (zh <= hsZdZh - t[i + 1]))
                {
                    tmp_sjg = h + (zh - hsQdZh) * zp[i + 1];
                }
                else if ((zh > hsZdZh - t[i + 1]) & (zh < hsZdZh + t[i + 1]))
                {
                    double x = 0;

                    x = zh - (hsZdZh - t[i + 1]);


                    if (w[i + 1] > 0)
                    {
                        tmp_sjg = h + (zh - hsQdZh) * zp[i + 1] + x * x / 2 / realBps[i + 1].ShuQuXian;

                    }
                    else if (w[i + 1] < 0)
                    {
                        tmp_sjg = h + (zh - hsQdZh) * zp[i + 1] - x * x / 2 / realBps[i + 1].ShuQuXian;

                    }


                }

                else if (zh > realBps[realBps.Count - 1].ZhuangHao)
                {
                    tmp_sjg = realBps[realBps.Count - 1].BpdGao + (zh - realBps[realBps.Count - 1].ZhuangHao) * zp[zp.Count - 1];

                }
                else if (zh < realBps[0].ZhuangHao)
                {
                    tmp_sjg = realBps[0].BpdGao - (realBps[0].ZhuangHao - zh) * zp[0];

                }

            }


            return tmp_sjg;

        }


        /// <summary>
        /// 计算桩号处的纵坡
        /// </summary>
        /// <param name="zh"></param>
        /// <returns></returns>
        public double CalSsZp(double zh)
        {

            double zp = 0;

            for (int i = 0; i <= realBps.Count - 2; i++)
            {

                if (zh >= (realBps[i].ZhuangHao + T[i]) & (zh <= realBps[i + 1].ZhuangHao - T[i + 1]))
                {
                    zp = Zp[i + 1];
                }
                else if ((zh > realBps[i + 1].ZhuangHao - T[i + 1]) & (zh < realBps[i + 1].ZhuangHao + T[i + 1]))
                {
                    double x = 0;

                    x = zh - (realBps[i + 1].ZhuangHao - T[i + 1]);

                    if (W[i + 1] > 0)
                    {
                        zp = Zp[i + 1] + x / realBps[i + 1].ShuQuXian;
                    }
                    else
                    {
                        zp = Zp[i + 1] - x / realBps[i + 1].ShuQuXian;
                    }

                }
                else if (zh > realBps[realBps.Count - 1].ZhuangHao)
                {
                    zp = 9999;


                }
                else if (zh < realBps[0].ZhuangHao)
                {
                    zp = 9999;

                }



            }

            return zp;

        }
        /// <summary>
        /// 将给定的桩号换算为路线上的实际桩号，一个给定桩号可能在不同的路段内，所以结果不一定唯一
        /// </summary>
        /// <param name="zh"></param>
        /// <returns></returns>
        public List<double> GetRealZhs(double zh)
        {

            List<double> tmpZhs = new List<double>();

            tmpZhs.Clear();

            if (dl.Count < 1)
            {
                tmpZhs.Add(zh);

            }
            else
            {
                int m = 0;

                int n = realBps[0].DlNo;

                for (int i = 0; i < ld.Count; i++)
                {
                    if (ld[i].Containzh(zh))
                    {
                        m = i + 1;

                        double tzZh = 0;

                        for (int j = n - 1; j < m - 1; j++)
                        {

                            if (dl[j].IsLong)
                            {
                                tzZh = tzZh + dl[j].AbsLong;
                            }

                            if (dl[j].IsShort)
                            {
                                tzZh = tzZh - dl[j].AbsLong;
                            }

                        }

                        tmpZhs.Add(zh + tzZh);

                    }

                }


            }

            return tmpZhs;

        }

        public double GetRealZh(double zh, LuDuan a_ld)
        {

            double tmpZh = 0;

            int n = 0;

            if (ld.Contains(a_ld))
            {
                n = ld.IndexOf(a_ld);


                double tzzh = 0;


                for (int i = 0; i < n; i++)
                {
                    if (dl[i].IsShort)
                    {

                        tzzh = tzzh - dl[i].AbsLong;

                    }
                    else
                    {

                        tzzh = tzzh + dl[i].AbsLong;

                    }

                }


                tmpZh = zh + tzzh;

            }

            return tmpZh;

        }



        public double GetDisplayZh(double zh)
        {

            double tmpZh = 0;

            if (dl.Count < 1)
            {
                tmpZh = zh;

            }
            else
            {

                double l = zh - realBps[0].ZhuangHao;

                double ll = 0;

                double lb = 0;

                double lc = 0;

                int n = realBps[0].DlNo;

                tmpZh = realBps[0].ZhuangHao;

                double la = ld[n - 1].Zdzh - realBps[0].ZhuangHao;

                if (l < la)
                {
                    tmpZh = zh;

                }
                else
                {

                    if (l > la && l < la + ld[n].Zdzh - ld[n].Qdzh)
                    {
                        tmpZh = ld[n].Qdzh + l - la;
                    }

                    lb = la;

                    for (int i = n; i < ld.Count - 1; i++)
                    {
                        lb = lb + ld[i].Zdzh - ld[i].Qdzh;

                        lc = lb + ld[i + 1].Zdzh - ld[i + 1].Qdzh;

                        if (l >= lb && l <= lc)
                        {
                            tmpZh = ld[i + 1].Qdzh + l - lb;

                            break;

                        }


                    }

                }

            }

            return tmpZh;

        }


        //public double Jssjg(double zh,double zl)
        //{


        //    for (int i = 0; i <= bps.Count - 2; i++)
        //    {

        //        if (zh >= (bps[i].ZhuangHao + t[i]) & (zh <= bps[i + 1].ZhuangHao - t[i + 1]))
        //        {
        //            return (bps[i].BpdGao + (zh - bps[i].ZhuangHao) * zp[i + 1]);

        //            //TextBox4.Text = Format((Zdmsj(i, 1) + (zh - Zdmsj(i, 0)) * Zp(i + 1)), "##0.0###")
        //        }
        //        else if ((zh > bps[i + 1].ZhuangHao - t[i + 1]) & (zh < bps[i + 1].ZhuangHao + t[i + 1]))
        //        {
        //            double x = 0;

        //            x = zh - (bps[i + 1].ZhuangHao - t[i + 1]);


        //            if (w[i + 1] > 0)
        //            {
        //                return (bps[i].BpdGao + (zh - bps[i].ZhuangHao) * zp[i + 1] + x * x / 2 / bps[i + 1].ShuQuXian);

        //            }
        //            else if (w[i + 1] < 0)
        //            {
        //                return (bps[i].BpdGao + (zh - bps[i].ZhuangHao) * zp[i + 1] - x * x / 2 / bps[i + 1].ShuQuXian);

        //            }


        //        }
        //        else if (zh > bps[bps.Count - 1].ZhuangHao)
        //        {
        //            return (bps[bps.Count - 1].BpdGao + (zh - bps[bps.Count - 1].ZhuangHao) * zp[zp.Count - 1]);

        //        }
        //        else if (zh < bps[0].ZhuangHao)
        //        {
        //            return (bps[0].BpdGao - (bps[0].ZhuangHao - zh) * zp[0]);

        //        }

        //    }

        //    return 0.00000000001;

        //}




        public double Jsdmg(double zh)
        {
            double dmg = 0;

            for (int i = 0; i < dmxs.Count - 1; i++)
            {
                if (zh >= dmxs[i].DmxZhuangHao && zh <= dmxs[i + 1].DmxZhuangHao)
                {
                    dmg = dmxs[i].DmxGaoCheng + (zh - dmxs[i].DmxZhuangHao) * (dmxs[i + 1].DmxGaoCheng - dmxs[i].DmxGaoCheng) / (dmxs[i + 1].DmxZhuangHao - dmxs[i].DmxZhuangHao);
                }


            }

            if (zh < dmxs[0].DmxZhuangHao)
            {
                dmg = dmxs[0].DmxGaoCheng;
            }
            if (zh >= dmxs[dmxs.Count - 1].DmxZhuangHao)
            {
                dmg = dmxs[dmxs.Count - 1].DmxGaoCheng;
            }


            return dmg;

        }

        //public double ZhModify(double zh)
        //{
        //    double tmpZh = 0;

        //    return tmpZh;
        //}

        public void CalSS(double zh, out string Gb, out string Laa, out string Lab, out string Lac, out string Lbb, out string Lbc, out string Lba, out string Lca, out string Lcb, out string Lcc)
        {
            //string tag_Gb = "";

            Gb = "";
            Laa = "";
            Lab = "";
            Lac = "";
            Lbb = "";
            Lbc = "";
            Lba = "";
            Lca = "";
            Lcb = "";
            Lcc = "";

            for (int i = 0; i <= realBps.Count - 2; i++)
            {

                if (zh >= (realBps[i].ZhuangHao + T[i]) & (zh <= realBps[i + 1].ZhuangHao - T[i + 1]))
                {
                    Gb = "直线坡数据";

                    Laa = "直坡桩号";

                    Lab = "坡长";

                    Lac = string.Format("{0:N4}", realBps[i + 1].ZhuangHao - realBps[i].ZhuangHao);

                    Lbb = "直坡段长";

                    Lbc = string.Format("{0:N4}", realBps[i + 1].ZhuangHao - T[i + 1] - (realBps[i].ZhuangHao + T[i]));

                    Lba = string.Format("{0:N4}", realBps[i].ZhuangHao + T[i]);

                    Lca = string.Format("{0:N4}", realBps[i + 1].ZhuangHao - T[i + 1]);

                    Lcb = "纵坡";

                    Lcc = string.Format("{0:P2}", Zp[i + 1]);



                }
                else if ((zh > realBps[i + 1].ZhuangHao - T[i + 1]) & (zh < realBps[i + 1].ZhuangHao + T[i + 1]))
                {
                    double x = 0;

                    x = zh - (realBps[i + 1].ZhuangHao - T[i + 1]);

                    Gb = "曲线坡数据";

                    Laa = "曲坡桩号";

                    Lba = string.Format("{0:N4}", realBps[i + 1].ZhuangHao - T[i + 1]);

                    Lca = string.Format("{0:N4}", realBps[i + 1].ZhuangHao + T[i + 1]);

                    Lab = "曲线半径";

                    Lac = string.Format("{0:N4}", realBps[i + 1].ShuQuXian);

                    Lbb = "T长";

                    Lbc = string.Format("{0:N4}", T[i + 1]);


                    Lcb = "瞬时坡";

                    x = zh - (realBps[i + 1].ZhuangHao - T[i + 1]);

                    if (W[i + 1] > 0)
                    {
                        Lcc = string.Format("{0:P2}", Zp[i + 1] + x / realBps[i + 1].ShuQuXian);
                    }
                    else
                    {

                        Lcc = string.Format("{0:P2}", Zp[i + 1] - x / realBps[i + 1].ShuQuXian);

                    }



                }
                else if (zh > realBps[bps.Count - 1].ZhuangHao)
                {
                    Gb = "超出范围";
                    Laa = "";
                    Lab = "";
                    Lac = "";
                    Lba = "";
                    Lbb = "";
                    Lbc = "";
                    Lca = "";
                    Lcb = "";
                    Lcc = "";

                }
                else if (zh < realBps[0].ZhuangHao)
                {
                    Gb = "超出范围";
                    Laa = "";
                    Lab = "";
                    Lac = "";
                    Lba = "";
                    Lbb = "";
                    Lbc = "";
                    Lca = "";
                    Lcb = "";
                    Lcc = "";

                }

            }



        }








        #endregion









    }

    public class LuDuan
    {

        private double _qdzh = 0;

        public double Qdzh
        {
            get { return _qdzh; }
            set { _qdzh = value; }
        }

        private double _zdzh = 0;

        public double Zdzh
        {
            get { return _zdzh; }
            set { _zdzh = value; }
        }

        public LuDuan(double zh_qd, double zh_zd)
        {

            _qdzh = zh_qd;

            _zdzh = zh_zd;

        }

        public LuDuan()
        {
            _qdzh = 0;

            _zdzh = 0;
        }

        public override string ToString()
        {
            return _qdzh.ToString() + "|" + _zdzh.ToString();
        }

        public bool Containzh(double zh)
        {
            bool b = false;

            if (zh >= _qdzh && zh <= _zdzh)
            {
                b = true;
            }

            return b;
        }





    }

    public class Duanlian
    {
        private double _dlQzh = 0;

        public double DlqZh
        {
            get { return _dlQzh; }
            set { _dlQzh = value; }
        }

        private double _dlHzh = 0;

        public double DlhZh
        {
            get { return _dlHzh; }
            set { _dlHzh = value; }
        }

        private double _absLong = 0;

        public double AbsLong
        {
            get
            {

                if (this.IsLong)
                {
                    return _dlQzh - _dlHzh;
                }

                if (this.IsShort)
                {
                    return _dlHzh - _dlQzh;
                }

                return 0;
            }

        }





        public Duanlian()
        {
        }

        public bool IsShort
        {
            get
            {
                return (DlhZh >= DlqZh);
            }

        }

        public bool IsLong
        {
            get
            {
                return (DlhZh <= DlqZh);
            }

        }

        public bool IsInShort(double zh)
        {
            bool b = false;

            if (zh > _dlQzh && zh < _dlHzh)
            {
                b = true;

            }

            return b;
        }

        public bool IsInLong(double zh)
        {
            bool b = false;

            if (zh > _dlHzh && zh < _dlQzh)
            {
                b = true;

            }

            return b;
        }

        public Duanlian(string str)
        {
            str = str.Trim();
            str = str.Trim('|');

            string[] dljs = str.Split('|');

            //for (int i = 0; i < dljs.GetLength(0); i++)
            //{

            //    MessageBox.Show(dljs.GetLength(0).ToString());
            //}




            if (dljs.GetLength(0) >= 2)
            {

                _dlQzh = Math.Round(Convert.ToDouble(dljs[0].ToString()), 3);

                _dlHzh = Math.Round(Convert.ToDouble(dljs[1].ToString()), 3);

            }



            //if (dljs.GetLength(0) == 3)
            //{
            //    m_BpdShuUXian = Math.Round(Convert.ToDouble(dljs[2].Trim()), 3);
            //}
            //else
            //{
            //    m_BpdShuUXian = 0;
            //}


        }

        public Duanlian(double _qdzh, double _zdzh)
        {
            _dlQzh = _qdzh;

            _dlHzh = _zdzh;

        }


    }

    public class DmxShuJu
    {
        private double ZhuangHao;

        private double Gao;

        public DmxShuJu()
        {
        }

        public DmxShuJu(string str)
        {

            str = str.Trim();
            str = str.Trim('|');

            string[] sjs = str.Split('|');

            //for (int i = 0; i < sjs.GetLength(0); i++)

            //{

            //    MessageBox.Show(sjs.GetLength(0).ToString());

            //}

            this.ZhuangHao = Convert.ToDouble(sjs[0].ToString());

            this.Gao = Convert.ToDouble(sjs[1].ToString());

        }

        public DmxShuJu(double zh, double dmg)
        {

            this.ZhuangHao = zh; ;

            this.Gao = dmg;

        }



        public double DmxZhuangHao
        {
            get
            {
                return this.ZhuangHao;
            }
        }

        public double DmxGaoCheng
        {
            get
            {
                return this.Gao;
            }
        }




    }

    public class BianPoShuJu
    {
        //成员变量 

        private double m_BpdZhuangHao;

        private double m_BpdGao;

        private double m_BpdShuUXian;

        private int dlNo = 0;


        //构造函数

        public BianPoShuJu(string str)
        {
            str = str.Trim();
            str = str.Trim('|');

            string[] sjs = str.Split('|');

            //for (int i = 0; i < sjs.GetLength(0); i++)
            //{

            //    MessageBox.Show(sjs.GetLength(0).ToString ());
            //}

            double tmpZh = 0;

            bool b = double.TryParse(sjs[0].ToString(), out tmpZh);

            if (b)
            {
                m_BpdZhuangHao = tmpZh;

                dlNo = 0;
            }
            else
            {
                bool bb = false;

                int a_tmpInt = 0;

                bb = GetZhAndDlNo(sjs[0].ToString(), out tmpZh, out a_tmpInt);

                if (bb)
                {
                    m_BpdZhuangHao = tmpZh;

                    dlNo = a_tmpInt;
                }

            }



            //m_BpdZhuangHao = Math.Round(Convert.ToDouble(sjs[0].ToString()), 3);

            m_BpdGao = Convert.ToDouble(sjs[1].ToString());

            if (sjs.GetLength(0) == 3)
            {
                m_BpdShuUXian = Convert.ToDouble(sjs[2].Trim());
            }
            else
            {
                m_BpdShuUXian = 0;
            }


        }

        public BianPoShuJu(double zh, double gao, double sqx)
        {

            m_BpdZhuangHao = zh;

            m_BpdGao = gao;

            m_BpdShuUXian = sqx;


        }



        public BianPoShuJu(double zh, double gao, double sqx, int num)
        {

            m_BpdZhuangHao = zh;

            m_BpdGao = gao;

            m_BpdShuUXian = sqx;

            dlNo = num;


        }
        

        //属性定义

        public double ZhuangHao
        {
            get
            {
                return this.m_BpdZhuangHao;
            }
        }

        public double BpdGao
        {
            get
            {
                return this.m_BpdGao;
            }
        }

        public double ShuQuXian
        {
            get
            {
                return this.m_BpdShuUXian;
            }
        }

        public int DlNo
        {
            get { return dlNo; }

            set
            {
                dlNo = value;
            }

        }

        //方法
        public bool GetZhAndDlNo(string str, out double zh, out int dlNo)
        {
            bool b = false;

            string strTmp = str.Trim();

            int n = 0;

            n = str.IndexOf(@"(");

            strTmp = str.Replace(@"(", " ");

            strTmp = strTmp.Replace(@")", " ");

            strTmp = strTmp.Trim();

            string[] sj = strTmp.Split(' ');

            double tmpZh = 0;

            bool bb = double.TryParse(sj[0].ToString(), out tmpZh);

            int tmpNo = 0;

            bool bbb = int.TryParse(sj[1].ToString(), out tmpNo);

            if (bb & bbb)
            {
                zh = tmpZh;

                dlNo = tmpNo;

                b = true;
            }
            else
            {
                zh = 0;

                dlNo = 0;

                b = false;
            }

            return b;

        }

    }

    public class ExcelData
    {
        public string[,] GetExcelPaste()
        {
            checked
            {
                string[,] result;
                try
                {
                    string text = System.Windows.Forms.Clipboard.GetText();
                    if (string.IsNullOrEmpty(text))
                    {
                        result = null;
                    }
                    else
                    {
                        string[] array = text.Trim().Split(new char[] {
                            '\n'
                        });
                        string[] array2 = this.mergesame_t(array[0]).Split(new char[] {
                            '\t'
                        });
                        int num = array.Length;

                        //int num2 = array2.Length;

                        int num2 = 3;


                        string[,] array3 = new string[num, num2];

                        for (int i = 0; i < num; i++)
                        {
                            string[] array4 = this.mergesame_t(array[i]).Split(new char[] {
                                '\t'
                            });


                            for (int j = 0; j < num2; j++)
                            {
                                if (array4.Length == 2 && j == 2)
                                {

                                    array3[i, j] = "0";

                                }
                                else
                                {
                                    array3[i, j] = array4[j];
                                }

                            }
                        }
                        result = array3;
                    }
                }
                catch (SystemException var_9_D5)
                {
                    result = null;
                }
                return result;
            }
        }

        private string mergesame_t(string str)
        {
            bool flag = false;
            string text = "";
            checked
            {
                for (int i = 0; i < str.Length; i++)
                {
                    char c = str[i];
                    if (c != '\r')
                    {
                        if (c == '\t')
                        {
                            if (flag)
                            {
                                goto IL_55;
                            }
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                        text += c;
                    }
                IL_55:
                    ;
                }
                char c2 = text[text.Length - 1];
                if (c2 == '\t')
                {
                    text = text.Substring(0, text.Length - 1);
                }
                return text;
            }
        }
    }









}
